<template>
  <div id="">
    <UploadExcel :on-success="handleSuccess" />
  </div>
</template>

<script>
import { reqAddEmployeeBatch } from '@/api/employees'
import { reqGetEmployeeList } from '@/api/employees'
import dayjs from 'dayjs'
import obj from '@/constant/employees'
export default {
  name: 'Import',
  data() {
    return {}
  },
  computed: {},
  methods: {
    async handleSuccess({ header, results }) {
      console.log(header, results)
      if (this.$route.query.type === 'user') {
        this.handleEmployeeBatch(header, results)
      }
      if (this.$route.query.type === 'money') {
        console.log('工资的批量导入')
      }
      if (this.$route.query.type === 'social') {
        console.log('社保s的批量导入')
      }
    },
    async handleEmployeeBatch(header, results) {
      const userRelations = {
        入职日期: 'timeOfEntry',
        手机号: 'mobile',
        姓名: 'username',
        转正日期: 'correctionTime',
        工号: 'workNumber'
      }
      const arr = []
      results.forEach((item) => {
        const obj = {}
        for (const key in item) {
          const englishKey = userRelations[key]
          if (englishKey === 'timeOfEntry' || englishKey === 'correctionTime') {
            obj[englishKey] = this.formatExcelDate(item[key], '-')
          } else {
            obj[englishKey] = item[key]
          }
        }
        // console.log(obj)
        arr.push(obj)
      })
      console.log(arr)
      const res = await reqAddEmployeeBatch(arr)
      console.log(res)
      this.$message.success(res.message)
      this.$router.push('/employees')
    },
    formatExcelDate(numb, format) {
      const time = new Date(numb * 24 * 3600000 + 1) // 毫秒
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return (
          year +
          format +
          (month < 10 ? '0' + month : month) +
          format +
          (date < 10 ? '0' + date : date)
        )
      }
      return (
        year +
        (month < 10 ? '0' + month : month) +
        (date < 10 ? '0' + date : date)
      )
    },
    async handleExport() {
      const headersArr = [
        '姓名',
        '手机号',
        '入职日期',
        '聘用形式',
        '转正日期',
        '工号',
        '部门'
      ]
      const headersRelations = {
        姓名: 'username',
        手机号: 'mobile',
        入职日期: 'timeOfEntry',
        聘用形式: 'formOfEmployment',
        转正日期: 'correctionTime',
        工号: 'workNumber',
        部门: 'departmentName'
      }

      const {
        data: { rows }
      } = await reqGetEmployeeList(1, this.total)
      console.log(rows)

      const resultsArr = this.formatFn(rows, headersArr, headersRelations)

      import('@/vendor/Export2Excel').then((excel) => {
        console.log(excel)
        excel.export_json_to_excel({
          // 表格的表头
          header: headersArr,
          // 表格的主体数据(固定格式->二维数组)
          data: resultsArr,
          filename: '员工信息表',
          autoWidth: true, // 宽度自适应 非必填
          bookType: 'xlsx' // 文件格式 非必填
        })
      })
    },
    formatFn(rows, headersArr, headersRelations) {
      // rows 决定了有多少小数组
      // headersArr 决定了小数组有多少项

      const res = []

      rows.forEach(item => {
        const arr = []

        headersArr.forEach(key => {
          const englishKey = headersRelations[key]
          let val = item[englishKey]
          if (['timeOfEntry', 'correctionTime'].includes(englishKey)) {
            val = dayjs(val).format('YYYY年MM月DD日')
          }
          if (englishKey === 'formOfEmployment') {
            const { hireType } = obj
            const resultObj = hireType.find(v => v.id === +val)
            val = resultObj ? resultObj.value : '未知'
          }
          arr.push(val)
        })

        res.push(arr)
      })

      return res

      // return [
      //   [],
      //   []
      // ]
    }
  }
}
</script>
<style scoped></style>
